class Solution {
public:
    int totalFruit(vector<int>& f)
    {
        unordered_map<int, int> hash;
        int left = 0, right = 0;
        int ret = 0;
        while (right < f.size())
        {
            hash[f[right]]++;
            while (hash.size() > 2)
            {
                hash[f[left]]--;
                if (hash[f[left]] == 0) hash.erase(f[left]);
                left++;
            }
            ret = max(ret, right - left + 1);
            right++;
        }
        return ret;
    }
};